import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:xbr_gaode_amap/base/amap_flutter_base.dart';
// import 'package:xbr_gaode_navi_amap/amap/base/amap_flutter_base.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/model/info_item.dart';
import 'package:yt_devops/model/task_detail_model.dart';
import 'package:yt_devops/model/task_tail_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/utils/enums.dart';

class InstallTaskDetailModel extends TaskDetailModel<InstallTaskStatusType> {
  @override
  InstallTaskStatusType? status;

  String? siteName;

  InstallTaskDetailModel(
      {super.id,
      super.type,
      super.name,
      super.address,
      super.content,
      super.handlerBy,
      super.orgId,
      super.phone,
      super.timeOfAppointment,
      super.userId,
      super.userName,
      super.createName,
      super.handlerName,
      super.orgName,
      super.descPicUrl,
      super.processingResults,
      super.site,
      super.siteId,
      this.status,
      super.tails,
      super.closeTime,
      super.createBy,
      super.createTime,
      super.current,
      super.description,
      super.email,
      super.endTime,
      super.objId,
      super.processTime,
      super.remark,
      super.startTime,
      super.taskTailId,
      super.latitude,
      super.longitude,
      this.siteName});

  factory InstallTaskDetailModel.fromJson(Map<String, dynamic> json) =>
      InstallTaskDetailModel(
        id: json['id'],
        name: json['name'],
        address: json['address'],
        content: json['content'],
        handlerBy: json['handlerBy'],
        orgId: json['orgId'],
        phone: json['phone'],
        timeOfAppointment: json['timeOfAppointment'],
        userId: json['userId'],
        orgName: json['orgName'],
        handlerName: json['handlerName'],
        userName: json['userName'],
        createName: json['createName'],
        descPicUrl: json['descPicUrl'] ?? '',
        processingResults: json['processingResults'],
        startTime: json['startTime'],
        endTime: json['endTime'],
        site: json['site'],
        siteId: json['siteId'],
        siteName: json['siteName'],
        remark: json['remark'],
        tails: List.of(
              json['tails'],
            ).map((e) => TaskTailModel.fromJson(e)).toList() ??
            [],
        status:
            $enumDecodeNullable($InstallTaskStatusTypeEnumMap, json['status']),
        type: $enumDecodeNullable($TaskTypeEnumMap, json['type']),
        longitude: json['longitude'],
        latitude: json['latitude'],
      );

  List<InfoItem> get baseInfos {
    return [
      InfoItem(ResKey.workOrderCode.tr, id?.toString() ?? ''),
      InfoItem(ResKey.siteName.tr, siteName ?? ''),
      InfoItem(ResKey.installTitle.tr, name ?? ''),
      InfoItem(ResKey.installationDescription.tr, content ?? ''),
    ];
  }

  //安装信息
  List<InfoItem> get installInfos {
    return [
      InfoItem(ResKey.installManu.tr, orgName ?? ''),
      InfoItem(ResKey.installer.tr, handlerName ?? ''),
      InfoItem(ResKey.customerName.tr, userName ?? ''),
      InfoItem(ResKey.contactNumber.tr, phone ?? ''),
      InfoItem(ResKey.appointmentTime.tr, timeOfAppointment ?? ''),
      InfoItem(ResKey.customerAddress.tr, address ?? '',
          type: InfoType.address,
          latLng: latitude != null && longitude != null
              ? LatLng(latitude!, longitude!)
              : null),
      InfoItem(ResKey.siteBaseInfoRemark.tr, remark ?? ''),
      InfoItem(ResKey.imageAttachment.tr, pics(), type: InfoType.img),
    ];
  }

  // 附件
  String pics() {
    String pics = descPicUrl ?? '';
    if (tails != null) {
      tails!.forEach((element) {
        if (element.picture != null) {
          pics = descPicUrl! + ',' + element.picture!;
        }
      });
    }

    return pics;
  }

//   安装   create("创建安装工单", 0), //创建工单事件
// receive("接受安装工单", 1), //接收工单事件
// createSite("创建站点", 4), //创建站点事件
// refuse("拒绝安装工单",3), //拒接工单事件
// modify("修改工单", 5),   //修改工单事件
// finish("完成安装工单",2);  //完成工单事件
  List<TaskTailModel> get tasilInfos {
    var taillList = [
      TaskTailModel(name: ResKey.taskCreation.tr, eventId: 0),
      TaskTailModel(name: ResKey.workOrderRecept.tr, eventId: 1),
      TaskTailModel(name: ResKey.workOrderComplete.tr, eventId: 2),
    ];
    if (tails != null) {
      for (var i = 0; i < tails!.length; i++) {
        var eventId = tails![i].eventId!;
        if (eventId < 3) {
          tails![eventId].name = taillList[eventId].name;
          taillList[eventId] = tails![eventId];
        }
      }
    }

    return taillList;
  }
}
